package com.xudu.controller.impl;


import com.github.pagehelper.PageInfo;
import com.xudu.common.Result;
import com.xudu.controller.IStudentController;
import com.xudu.entity.Student;
import com.xudu.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xudu
 * @since 2024-04-21
 */
@RestController
@RequestMapping("/student")
public class StudentControllerImpl implements IStudentController {

    @Autowired
    private IStudentService iStudentService;




    @Override
    public Result add(Student student){
        boolean b = iStudentService.add(student);
        if (b){
            return Result.success(student);
        }else {
            return Result.error("添加学生失败！");
        }

    }

    @Override
    public Result delete(Student student) {
        boolean b = iStudentService.del(student);
        if (b){
            return Result.success();
        }else {
            return Result.error("删除学生失败！");
        }

    }

    @Override
    public Result batchDelete(List<Long> ids) {
        //List<Integer> list1 = Arrays.asList(list);
        boolean b = iStudentService.batchdel(ids);
        if (b){
            return Result.success();
        }else {
            return Result.error("批量删除学生失败！");
        }
    }

    @Override
    public Result update(Student student) {
        boolean b = iStudentService.update(student);
        if (b){
            return Result.success();
        }else {
            return Result.error("修改学生失败！");
        }
    }

    @Override
    public Result getAll() {
        List<Student> studentList = iStudentService.getAll();
        if (studentList!=null){
            return Result.success(studentList);
        }else {
            return Result.error("查询失败");
        }

    }

    @Override
    public Result findByNum(Student student) {
        Student byNum = iStudentService.getByNum(student);
        if (byNum!=null){
            return Result.success(byNum);
        }else {
            return Result.error("查询失败！");
        }

    }

    @Override
    public Result fuzzySearchByName(String string) {
        List<Student> students = iStudentService.fuzzySearchByName(string);
        if (students!=null){
            return Result.success(students);
        }else {
            return Result.error("没有包含【"+string+"】字的学生！");
        }

    }

    @Override
    public PageInfo<Student> page(@RequestParam Integer current, @RequestParam Integer size){
        return iStudentService.page(current, size);
    }
}
